package com.thread;

import java.util.Arrays;
import java.util.concurrent.CountDownLatch;

public class JUC_CountDownLatch_Join_17 {

    public static void main(String[] args) {
        countDownLatch();
    }

    static void countDownLatch(){
        Thread[] ts = new Thread[100];
        CountDownLatch countDownLatch = new CountDownLatch(ts.length);
        Arrays.stream(ts).forEach((thread -> {
            System.out.println(Thread.currentThread().getName()+ " count downlatch ...");
            countDownLatch.countDown();
        }));

        try {
            System.out.println(countDownLatch.getCount());
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(" count down end...");

    }

    static void join(){
        Thread[] ts = new Thread[100];
        CountDownLatch countDownLatch = new CountDownLatch(ts.length);
        Arrays.stream(ts).forEach((thread -> {
            System.out.println(Thread.currentThread().getName()+ " count downlatch ...");
            countDownLatch.countDown();
        }));

        Arrays.stream(ts).forEach((thread -> {
            try {
                thread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }));

        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(" join down end...");

    }

}
